﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Bricks.Business.Service.Impl;
using Bricks.Site.Models;
using Bricks.Business.Models.FlowProcess;
using System.Xml;
using System.Web.Script.Serialization;
using Bricks.Business.Service;
using Bricks.Component.Utils;
using Bricks.Site.Web.CommonUtils;

namespace Bricks.Site.Web.Controllers
{
    [Export]
    public class HolidayRequestController : BaseController
    {
        [Import]
        public IHolidayRequestService HolidayRequestService { get; set; }
        [Import]
        public IFlowUtilsService ProcessUtilsService { get; set; }

        public ActionResult HolidayRequest(string processID)
        {
            if (string.IsNullOrEmpty(processID))
            {
                HolidayRequestView holidayRequestView = new HolidayRequestView();
                holidayRequestView.ID = Guid.NewGuid();
                holidayRequestView.RequestPerson = User.Identity.Name;
                holidayRequestView.HandleState = "0";
                holidayRequestView.RequestStartDate = DateTime.Now;
                holidayRequestView.RequestEndDate = DateTime.Now.AddDays(1);
                return View(holidayRequestView);
            }
            else
            {
                HolidayRequestView holidayRequestView = HolidayRequestService.GetHolidayRequestByID(processID);
                return View(holidayRequestView);
            }
        }

        [HttpPost]
        public ActionResult SaveHoliday()
        {
            try
            {
                string id = Request.Params["id"];
                string requestReason = Request.Params["requestReason"];
                string requestStartDate = Request.Params["requestStartDate"];
                string requestEndDate = Request.Params["requestEndDate"];
                DateTime dtStartDate = Convert.ToDateTime(requestStartDate);
                DateTime dtEndDate = Convert.ToDateTime(requestEndDate);
                HolidayRequestService.SaveHoliday(Guid.Parse(id), User.Identity.Name, requestReason, dtStartDate, dtEndDate);
                return Content("Success");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        public ActionResult CancelHoliday(string id)
        {
            try
            {
                ProcessUtilsService.Cancel("HolidayRequest",User.Identity.Name, id);
                HolidayRequestService.UpdateHoliday(Guid.Parse(id), "", DateTime.Now, DateTime.Now, "3", true);
                return Content("Success");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        public ActionResult GenerateGraph(string id)
        {
            HolidayRequestView holidayRequestView = HolidayRequestService.GetHolidayRequestByID(id);
            string handleState = "";
            if (holidayRequestView != null)
            {
                handleState = holidayRequestView.HandleState;
            }
            WorkFlowView workFlowView = ProcessUtilsService.GenerateGraph("HolidayRequest", id, handleState);
            return Json(workFlowView, JsonRequestBehavior.AllowGet);
        }

        public ActionResult HolidayRequestList()
        {
            ViewBag.PanelInfo = base.GetPanelInfo();
            return View();
        }

        public ActionResult GetHolidayRequestList()
        {
            return Json(HolidayRequestService.GetHolidayRequestByRequestPerson(User.Identity.Name), JsonRequestBehavior.AllowGet);
        }
    }
}